package com.example.day04.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.day04.pojo.User;
import com.example.day04.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @RequestMapping("/findAll")
    public String findAll(Model model,Integer pageIndex,String name){
        if (pageIndex==null) pageIndex=1;
        Page<User> page = userService.findAll(pageIndex,name);
        model.addAttribute("name",name);
        model.addAttribute("page",page);
        return "show";
    }

    @RequestMapping("toAdd")
    public String toAdd(){
        return "add";
    }

    @RequestMapping("add")
    public String add(User user){
        userService.add(user);
        return "redirect:findAll";
    }

    @RequestMapping("findById")
    public String findById(Integer id,Model model){
        User user = userService.findById(id);
        model.addAttribute("user",user);
        return "update";
    }

    @RequestMapping("update")
    public String update(User user){
        userService.update(user);
        return "redirect:findAll";
    }

    @RequestMapping("delete")
    public String delete(Integer id){
        userService.delete(id);
        return "redirect:findAll";
    }
}
